//
//  ViewController.m
//  KVODemo
//
//  Created by Lim on 2021/3/11.
//

#import "ViewController.h"
#import "MyPerson.h"
@interface ViewController ()
@property (nonatomic, strong) MyPerson *person;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.person = [MyPerson new];
    self.person.age = 10;
    NSLog(@"%p", [self.person methodForSelector:@selector(setAge:)]);
    [self.person addObserver:self forKeyPath:@"age" options: NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"111"];
    // Do any additional setup after loading the view.
    NSLog(@"%p", [self.person methodForSelector:@selector(setAge:)]);

}

- (void)dealloc
{
    [self.person removeObserver:self forKeyPath:@"age"];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.person.age = 22;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == @"111") {
        NSLog(@"%@=%@= %@= %@", keyPath, object, change, context);
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

@end
